package com.zimsoft.strato.admin.deploy.v1_0_0.comm.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.druid.stat.JdbcSqlStat;
import com.alibaba.druid.stat.JdbcSqlStatValue;
import com.strato.jdbc.datasource.v1_0_0.DataSourceFactory;
import com.strato.mvc.annotation.v1_0_0.annotation.method.RequestMapping;
import com.strato.mvc.annotation.v1_0_0.annotation.type.Controller;
import com.strato.mvc.api.v1_0_0.model.ModelAndView;

@Controller
@com.strato.mvc.annotation.v1_0_0.annotation.type.RequestMapping("${strato.admin.url.prefix}")
public class MonitorController {

	@RequestMapping("/monitor/dsInfo")
	public ModelAndView dsInfo(HttpServletRequest request,ModelAndView mav){
		Map<Object,Object> modelMap = new HashMap<Object,Object>();
		DruidDataSource ds = (DruidDataSource) DataSourceFactory.getInstance().getDataSource("deploy");
		Map<String,Object> statMap = ds.getStatDataForMBean();
		
		//获得一个连接池，让druidDS初始化
		DruidPooledConnection conn = null;
		try {
			conn = ds.getConnection();
			System.out.println(conn);
			System.out.println(ds.getWallStatMap());
		} catch (SQLException e) {
			
		}finally{
			if(conn != null){
				try {
					conn.close();
				} catch (SQLException e) {
				}
				conn = null;
			}
		}
		
		List<Map<String,Object>> dsList = new ArrayList<Map<String,Object>>();
		if(statMap != null && statMap.size() > 0){
			Map<String,Object> dsMap = new HashMap<String,Object>();
			dsMap.put("name", "strato.admin");
			List<Map<String,Object>> statInfoMapList = new ArrayList<Map<String,Object>>();
			Set<String> keySet = statMap.keySet();
			for(String key:keySet){
				Map<String,Object> statInfoMap = new HashMap<String,Object>();
				statInfoMap.put("key", key);
				statInfoMap.put("value", statMap.get(key));
				statInfoMapList.add(statInfoMap);
			}
			dsMap.put("statInfoList", statInfoMapList);
			dsList.add(dsMap);
			modelMap.put("dsList", dsList);
		}
		
		mav.setModel(modelMap);
		mav.setViewName("dsInfo");
		return mav;
	}
	
	@RequestMapping("/monitor/sqlInfo")
	public ModelAndView sqlInfo(HttpServletRequest request,ModelAndView mav){
		Map<Object,Object> modelMap = new HashMap<Object,Object>();
		String dsName = request.getParameter("dsName");
		DruidDataSource ds = (DruidDataSource) DataSourceFactory.getInstance().getDataSource(dsName);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, JdbcSqlStat> sqlStatMap = ds.getSqlStatMap();
		if(sqlStatMap != null && sqlStatMap.size() > 0){
			Set<String> keySet = sqlStatMap.keySet();
			for(String key:keySet){
				Map<String,Object> statMap = new HashMap<String,Object>();
				JdbcSqlStat sqlStat = sqlStatMap.get(key);
				JdbcSqlStatValue statValue = sqlStat.getValue(false);
				String name = statValue.getName();
				statMap.put("sqlName", name);
				statMap.put("sqlStatement", statValue.getSql());
				
				statMap.put("exeCount", statValue.getExecuteCount());
				statMap.put("exeLastTime", sdf.format(statValue.getExecuteLastStartTime()));
				statMap.put("exeErrorCount", statValue.getExecuteErrorCount());
				if(statValue.getExecuteErrorLastTime()!=null){
					statMap.put("exeErrorLastTime", sdf.format(statValue.getExecuteErrorLastTime()));
				}
				statMap.put("exeMillisMax", statValue.getExecuteMillisMax());
				statMap.put("exeSecondTotal", statValue.getExecuteMillisTotal()/1000);
				statMap.put("fetchRowCount", statValue.getFetchRowCount());
				statMap.put("fetchRowCountMax", statValue.getFetchRowCountMax());
				statMap.put("updateRowCount", statValue.getUpdateCount());
				statMap.put("updateRowCountMax", statValue.getUpdateCountMax());
				modelMap.put("statMap", statMap);
			}
			mav.setModel(modelMap);
		}
		
		
		mav.setViewName("sqlInfo");
		return mav;
	}
}
